package com.freemyleft.left.zapp.wechat.templates.leftwechat;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.freemyleft.left.zapp.net.RestClient;
import com.freemyleft.left.zapp.net.callback.IError;
import com.freemyleft.left.zapp.net.callback.IFailure;
import com.freemyleft.left.zapp.net.callback.ISuccess;
import com.freemyleft.left.zapp.util.LeftLogger;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.modelmsg.SendAuth;

/**
 * Created by FreeMyleft on 2017/12/15.
 */
///
public abstract class BaseWxEntryActivity extends BaseWXActivity {
    //用户成功登录后的回调
    protected abstract void onSignInSuccess(String  userInfo);


    //微信发送请求到第三方应用后的回调
    @Override
    public void onReq(BaseReq baseReq) {

    }

    //第三方应用发送请求到微信的回调
    @Override
    public void onResp(BaseResp baseResp) {
        final String code=((SendAuth.Resp)baseResp).code;
        final StringBuilder authUrl=new StringBuilder();
        authUrl
                .append("https://api.weixin.qq.com/sns/oauth2/access_token?appid=")
                .append(LeftWeChat.APP_ID)
                .append("&secret=")
                .append(LeftWeChat.APP_SECRET)
                .append("&code=")
                .append(code)
                .append("&grant_type=authorization_code");
        LeftLogger.d("authUrl",authUrl.toString());


    }
    private void getAuth(final String authUrl){
        RestClient
                .builder()
                .url(authUrl)
                .success(new ISuccess() {
                    @Override
                    public void onSuccess(String responnse) {
                        //这里面会拿到isuccesstoken和openid
                        final JSONObject authObject= JSON.parseObject(responnse);
                        final String accessToken=authObject.getString("access_token");
                        final String openId=authObject.getString("openid");

                        final  StringBuilder userInfoUrl=new StringBuilder();
                        userInfoUrl
                                .append("https://api.weixin.qq.com/sns/userinfo?access_token=")
                                .append(accessToken)
                                .append("&openid=")
                                .append(openId)
                                .append("lang=")
                                .append("zh_CN");
                        LeftLogger.d("userInfo",userInfoUrl.toString());

                    }
                }).buid().get();
    }

    private void getUserInfo(String  userInfoUrl){
        RestClient
                .builder()
                .url(userInfoUrl)
                .success(new ISuccess() {
                    @Override
                    public void onSuccess(String responnse) {
                        onSignInSuccess(responnse);

                    }
                }).failure(new IFailure() {
            @Override
            public void onFailure() {
                //正式项目开发的时候

            }
        }).error(new IError() {
            @Override
            public void onError(int code, String msg) {

            }
        })
                .buid().get();
    }
}
